/*****************************************************************
 * created by lei.he on 2020/07/29 for userRouter file
******************************************************************/

 /** 引入日志助手 */
const {HandleLogs,CrpytoMD5,UnCrpytoAES} = require('../utils');
const userScheme = require('../schema/userSchema');


exports.UserRouter = app => {
    /** 用户登录接口 */
    app.post('/user/login',(req,res) =>{
        HandleLogs('post uese/login 用户登录接口',req.body)
        let data = req.body

        userScheme.findOne({userId:data.userId},(err,doc) => {
            if (!!err) {
                HandleLogs('post uese/login 服务器错误',err)
                res.send({code:500,message : '登录失败，服务器错误'})
            } else {
                HandleLogs('post user/login 登录成功',doc)
                    if (!!doc) {
                        if(doc.state === '1'){
                            var newUserPwd = CrpytoMD5(doc.userPwd),
                                token = CrpytoMD5(doc.userId+'.'+newUserPwd+'.'+Date.now())
                            if(newUserPwd == data.userPwd){
                                res.send({code:200,message:'',data:{
                                    userId:doc.userId,
                                    userName : doc.userName,
                                    userTele : doc.userTele,
                                    token 
                                }})
                            }else{
                                res.send({code:400,message:'密码错误'})
                            }
                        }else{
                            res.send({code:400,message:'用户已注销'})
                        }
                    } else {
                        res.send({code:400,message:'用户名错误或不存在'})
                    }
                
            }
        })
    })

    /** 用户列表接口 */
    app.get('/user/list',(req,res) => {
        HandleLogs('GET USER/LIST 用户列表查询')
        let data = req.query,
            reg = new RegExp(data.userName,'i')
        userScheme.find({userName:{$regex: reg},state:1},(err,doc) => {
            if (!!err) {
                HandleLogs('GET USER/LIST 服务器错误',err)
                res.send({code:500,message:'查询失败,服务器错误'})
            } else {
                HandleLogs('GET USER/LIST 查询成功',doc)
                if (!!doc) {
                    res.send({code:200,message:'',body:doc})
                } else {
                    res.send({code:400,message:'用户不存在'})
                }
            }
        })
    })
    /** 添加用户接口 */
    app.post('/user/add',(req,res) => {
        HandleLogs('POST USER/ADD 用户新增列表',req.body)
        let jmuser = req.body,
            headers = req.headers   
        var data = UnCrpytoAES(jmuser.str,headers.token)   
            HandleLogs('用户信息解密-----------',data)
        userScheme.find({userId : data.userId},(err,doc) => {
            if (!!err) {
                HandleLogs('GET USER/LIST 服务器错误',err)
            } else {
                HandleLogs('GET USER/LIST 查询成功',doc)
                if (doc.length>0) {
                    res.send({code:400,message:'用户ID已存在,请重新输入'})
                } else {
                    var userModel = new userScheme(data)
                    userModel.save((err,doc) => {
                        if (!!err) {
                            HandleLogs('POST USER/ADD 服务器错误',err)
                            res.send({code:500,message:'新增失败,服务器错误'})
                        } else {
                            HandleLogs('GET USER/LIST 新增用户信息成功',doc)
                            res.send({code:200,message:'新增用户信息成功',body:doc})
                        }
                    })
                }
            }
        })
        
    })
    /** 用户修改接口 */
    app.put('/user/:id',(req,res) => {
        let userId = req.params.id,
            updata = req.body
        userScheme.update({userId},updata,(err,doc) => {
            if (!!err) {
                HandleLogs('PUT USER/:ID 服务器错误',err)
                res.send({code:500,message:'修改失败,服务器错误'})
            } else {
                HandleLogs('GET USER/LIST 修改用户信息成功',doc)
                res.send({code:200,message:'修改用户信息成功',body:doc})
            }
        })   
    })
    /** 用户删除接口 */
    app.delete('/user/:id',(req,res) => {
        let userId = req.params.id,
            dedata = req.body
        userScheme.update({userId},dedata,(err,doc) => {
            if (!!err) {
                HandleLogs('PUT USER/:ID 服务器错误',err)
                res.send({code:500,message:'注销用户失败,服务器错误'})
            } else {
                HandleLogs('GET USER/LIST 注销用户成功',doc)
                res.send({code:200,message:'注销用户成功',body:doc})
            }
        })       
        // userScheme.remove({userId},(err,doc) => {
        //     if(!!err){
        //         HandleLogs('DELETE USER/:ID 服务器错误',err)
        //         res.send({code:500,message:'修改失败,服务器错误'})
        //     } else {
        //         HandleLogs('GET USER/LIST 删除用户信息成功',doc)
        //         res.send({code:200,message:'',body:doc})
        //     }
        // })

    })
}